package ro.msg.office.beans;

import java.sql.Time;

import java.text.ParseException;


import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import ro.msg.office.ticketmanager.DO.TicketDO;
import ro.msg.office.ticketmanager.back.ifc.TicketManager;


public class CreateTicketBean {

    private Date date;
    private Date startTimeS;
    private Date endTimeS;
    private String description;
    GregorianCalendar calendar;
    @EJB
    TicketManager ticketEJB;

    /**
     * @param date        -specifies the date when the permission is needed
     * @param startTimeS  -the hour of the day when the permission starts
     * @param endTimeS    -the hour of the day when the permission ends
     * @param description - reason for wanting the permission
     */
    public CreateTicketBean(Date date, Date startTimeS, Date endTimeS, String description) {
        super();
        this.date = date;
        this.startTimeS = startTimeS;
        this.endTimeS = endTimeS;
        this.description = description;

    }

    public CreateTicketBean() {
        // TODO Auto-generated constructor stub
    }

    /**
     * The setter and the getter methods are used to make the state of the managed bean accessible.
     *The setter methods are used to assign a value to a bean's property.
     *The getter methods are used to obtain the value of the property from a bean. 
     */
    public void setDate(Date date) {
        this.date = date;
    }

    public Date getDate() {
        return date;
    }

    public void setStartTimeS(Date startTimeS) {
        this.startTimeS = startTimeS;
    }

 

    public Date getStartTimeS() {

        return startTimeS;
    }

    public void setEndTimeS(Date endTimeS) {
        this.endTimeS = endTimeS;
    }

    public Date getEndTimeS() {
        return endTimeS;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    /**This method is responsible with the creation of the ticket when the user fills up correctly the fields provided on the web page.
     * It is connected to the send button in the createTicket.xhtml file used for submitting the form
     * @returns a String whether the ticket was successfully created or not
     */
   
    public String createTicket()  {

        if ((this.startTimeS == null) || (this.endTimeS == null) || (this.description == null)
                || (this.description.trim().length() == 0) || (this.date == null)) {
            FacesContext facesContext = FacesContext.getCurrentInstance();
            FacesMessage fm = new FacesMessage("Input incorrect!" + "--" + this.startTimeS + "--" + this.endTimeS
                    + "--" + this.description + "--" + this.date);
            facesContext.addMessage(null, fm);
            return "errorCreateTicket";
        } else {

            try {
                startTimeS.setMonth(date.getMonth());
                startTimeS.setDate(date.getDate());
                startTimeS.setYear(date.getYear());
                endTimeS.setMonth(date.getMonth());
                endTimeS.setDate(date.getDate());
                endTimeS.setYear(date.getYear());
                if ((this.startTimeS.getHours() >= 8) && (this.startTimeS.getHours() <= 19)
                        && (this.endTimeS.getHours() >= 9) && (this.endTimeS.getHours() <= 20)
                        && (this.startTimeS.getMinutes() >= 0) && (this.startTimeS.getMinutes() <= 60)
                        && (this.endTimeS.getMinutes() >= 0) && (this.endTimeS.getMinutes() <= 60)
                        && this.startTimeS.getHours() <= this.endTimeS.getHours()) {

                    TicketDO ticket = new TicketDO();
                    ticket.setCreationDate(new Date());
                    ticket.setDescription(this.description);
                    ticket.setStartTime(this.startTimeS);
                    ticket.setEndTime(this.endTimeS);
                    ticket.setUserID(LoginBean.getUser().getId());
                    ticket.setStatus(new Long(1));

                    if (ticketEJB.createTicket(ticket)) {
                        FacesMessage fm = new FacesMessage("Success!");
                        FacesContext.getCurrentInstance().addMessage(null, fm);
                        init();

                        FacesContext facesContext = FacesContext.getCurrentInstance();
                        FacesMessage fm1 = new FacesMessage("oke!");
                        facesContext.addMessage(null, fm1);
                        return "oke";
                    } else {
                        FacesMessage fm = new FacesMessage("something went wrong");
                        FacesContext.getCurrentInstance().addMessage(null, fm);
                        //return "something went wrong";
                        return "errorCreateTicket";

                    }
                } else {
                    FacesMessage fm = new FacesMessage("Error: Incorrect input!");
                    FacesContext.getCurrentInstance().addMessage(null, fm);
                    return "errorCreateTicket";
                }
            } catch (NumberFormatException e) {
                FacesMessage fm = new FacesMessage("Error: Incorrect input!");
                FacesContext.getCurrentInstance().addMessage(null, fm);
                return "errorCreateTicket";
            }

        }

    }

    /**This method helps the user save the data that was already filled up if something happens and he doesn't have the chance to finish the form
     * @returns a String if the form was saved as a draft or not
    */
    public String saveDraft()  {

        if ((this.startTimeS == null) || (this.endTimeS == null) || (this.date == null)) {
            FacesContext facesContext = FacesContext.getCurrentInstance();
            FacesMessage fm = new FacesMessage("Input incorrect!");
            facesContext.addMessage(null, fm);
            return "errorCreateTicket";
        } else {

            try {
                startTimeS.setMonth(date.getMonth());
                startTimeS.setDate(date.getDate());
                startTimeS.setYear(date.getYear());
                endTimeS.setMonth(date.getMonth());
                endTimeS.setDate(date.getDate());
                endTimeS.setYear(date.getYear());
                if ((this.startTimeS.getHours() >= 8) && (this.startTimeS.getHours() <= 19)
                        && (this.endTimeS.getHours() >= 9) && (this.endTimeS.getHours() <= 20)
                        && (this.startTimeS.getMinutes() >= 0) && (this.startTimeS.getMinutes() <= 60)
                        && (this.endTimeS.getMinutes() >= 0) && (this.endTimeS.getMinutes() <= 60)) {

                    TicketDO ticket = new TicketDO();
                    ticket.setCreationDate(new Date());
                    ticket.setDescription(this.description);
                    ticket.setStartTime(this.startTimeS);
                    ticket.setEndTime(this.endTimeS);
                    ticket.setUserID(LoginBean.getUser().getId());
                    ticket.setStatus(new Long(0));

                    if (ticketEJB.createTicket(ticket)) {
                        FacesMessage fm = new FacesMessage("Success!");
                        FacesContext.getCurrentInstance().addMessage(null, fm);
                        init();

                        FacesContext facesContext = FacesContext.getCurrentInstance();
                        FacesMessage fm1 = new FacesMessage("Ticket has been saved as draft!");
                        facesContext.addMessage(null, fm1);
                        return "oke";
                    } else {
                        FacesMessage fm = new FacesMessage("Something went wrong");
                        FacesContext.getCurrentInstance().addMessage(null, fm);
                        //return "something went wrong";
                        return "errorCreateTicket";

                    }
                } else {
                    FacesMessage fm = new FacesMessage("Error: Incorrect input!");
                    FacesContext.getCurrentInstance().addMessage(null, fm);
                    return "errorCreateTicket";
                }
            } catch (NumberFormatException e) {
                FacesMessage fm = new FacesMessage("Error: Incorrect input!");
                FacesContext.getCurrentInstance().addMessage(null, fm);
                return "errorCreateTicket";
            }

        }

    }

    private TimeZone timeZone;

    /**This method gets the default TimeZone for this host.
      
     */
    public TimeZone getTimeZone() {
        timeZone = TimeZone.getDefault();
        return timeZone;
    }

    /**
     * This method resets all the fields for a new user or for creating a new ticket
     */
    public void init() {
        this.setStartTimeS(null);
        this.setEndTimeS(null);
        this.setDescription("");
        this.setDate(null);
    }
  
    /**
     * @return a String which is reffered to in the navigation file and redirects the user to the login page
     */
    public String signOut() {
        return "signOut";
    }
}
